package org.jeecg.modules.appuser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.Date.DateUtil;
import org.jeecg.modules.appuser.entity.TbUserIntegral;
import org.jeecg.modules.appuser.entity.TbUserIntegralConfigDetail;
import org.jeecg.modules.appuser.entity.Tb_user_contribution;
import org.jeecg.modules.appuser.mapper.TbUserIntegralConfigDetailMapper;
import org.jeecg.modules.appuser.service.ITbUserIntegralConfigDetailService;
import org.jeecg.modules.appuser.service.ITbUserIntegralService;
import org.jeecg.modules.appuser.service.ITb_user_contributionService;
import org.jeecg.modules.mall.entity.TbMnUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.*;

/**
 * @Description: 用户积分奖励明细表
 * @Author: jeecg-boot
 * @Date: 2020-05-19
 * @Version: V1.0
 */
@Service
@Slf4j
public class TbUserIntegralConfigDetailServiceImpl extends ServiceImpl<TbUserIntegralConfigDetailMapper, TbUserIntegralConfigDetail> implements ITbUserIntegralConfigDetailService {


    @Autowired
    @Lazy(true)
    private ITb_user_contributionService tb_user_contributionService;

    @Autowired
    private ITbUserIntegralService tbUserIntegralService;

    @Override
    public TbUserIntegralConfigDetail getOneByName(String taskName) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("task_name", taskName);
        return getOne(queryWrapper);
    }

    @Override
    public TbUserIntegralConfigDetail getTodayTaskByName(String taskName, String user_id) {

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("task_name", taskName);
        queryWrapper.eq("user_id", user_id);

        queryWrapper.le("create_time", new Date());
        Calendar date1 = Calendar.getInstance();
        queryWrapper.ge("end_time", new Date());
        //queryWrapper.eq("complete_state",1);
        return getOne(queryWrapper);

    }

    @Override
    public List<TbUserIntegralConfigDetail> getTodayTaskByNameList(String taskName, String user_id) {

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("task_name", taskName);
        queryWrapper.eq("user_id", user_id);

        queryWrapper.le("create_time", new Date());
        Calendar date1 = Calendar.getInstance();
        queryWrapper.ge("end_time", new Date());
        //queryWrapper.eq("complete_state",1);
        return list(queryWrapper);

    }

    @Override
    public Result<?> getLoginUserAllTasksAndIntegral(String user_id) {

        Map<String, Object> resultMap = new HashMap<>();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", user_id);
        log.info("\n\n\n\n\n\n---------------------------------------" + DateUtil.getCurrentTimeBySecond());
        queryWrapper.le("create_time", DateUtil.getCurrentTimeBySecond());
        queryWrapper.ge("end_time", DateUtil.getCurrentTimeBySecond());
        List<TbUserIntegralConfigDetail> tbUserIntegralConfigDetailList = list(queryWrapper);
        resultMap.put("taskList", tbUserIntegralConfigDetailList);
        TbUserIntegral tbUserIntegral = tbUserIntegralService.getOneByUserId(user_id);
        if (null == tbUserIntegral) {
            resultMap.put("integral", 0);
        } else {
            resultMap.put("integral", tbUserIntegral);
        }
        QueryWrapper queryWrapper_contri = new QueryWrapper();
        queryWrapper_contri.eq("user_id", user_id);
        Tb_user_contribution tb_user_contribution = tb_user_contributionService.getOne(queryWrapper_contri);
        //自动创建贡献值
        if (tb_user_contribution == null) {
            tb_user_contribution=new Tb_user_contribution();
            tb_user_contribution.setContributionValue(0);
            tb_user_contribution.setUserId(user_id);
            tb_user_contributionService.save(tb_user_contribution);
        }

        resultMap.put("tb_user_contribution", tb_user_contribution);

        return Result.ok(resultMap);
    }


}
